/****************************************************
 * 
 * home soft 版权所有.
 * 
 *****************************************************/

package com.homesoft.runtime.core;

import com.homesoft.runtime.config.Configuration;
import com.homesoft.runtime.config.SectionUtil;
import com.homesoft.runtime.config.SystemConfigSection;
import com.homesoft.runtime.log.ILog;
import com.homesoft.runtime.log.LogLevel;
import com.homesoft.runtime.log.LogManager;

/**
 * @author lihg
 * @data 2012-9-13
 */
public class SystemStartupListener extends AbstractRuntimeListener {

	private static final String MODULE_SYSTEM = SectionUtil.SYSTEM_SECTION;
	private static final String MODULE_LOG = SectionUtil.LOG_SECTION;
	private static final ILog logger = LogManager.getLogger(SystemStartupListener.class);

	@Override
	public void onStart(RuntimeEventArgs e) {
		logger.writeSystemLog(LogLevel.Info, "加载系统配置: OnStart");
		InitSysConfig(e.getConfig());
	}

	@Override
	public void onRefresh(RuntimeEventArgs e) {
		logger.writeSystemLog(LogLevel.Info, "重新加载系统配置: OnRefresh");
		InitSysConfig(e.getConfig());
	}

	/**
	 * @param config
	 */
	private void InitSysConfig(Configuration config) {
		if (config == null)
        {
            logger.writeTraceLog(LogLevel.Error, "传入配置对象参数为null");
            //throw new GTPConfigurationException(ConfigurationErrorCode.CONFIG_IS_NULL);
        }

        boolean sysIsExist = config.isExistSection(MODULE_SYSTEM);
        SystemConfigSection system = null;
        if (!sysIsExist)
        {
            logger.writeSystemLog(LogLevel.Info , String.format("配置文件中未找到系统配置组:{0}", MODULE_SYSTEM));
            system = new SystemConfigSection();
        }
        else
        {
            system = config.getSection(SystemConfigSection.class);
        }
        //TODO:
        //SystemInfo si = SystemInfo.Instance;
        //si.InitConfig(system);

        boolean logIsExist = config.isExistSection(MODULE_LOG);

        if (!logIsExist)
        {
            logger.writeSystemLog(LogLevel.Info, String.format("配置文件中未找到系统配置组:{0}。不进行任何配置操作。", MODULE_LOG));
            return;
        }
	}

	@Override
	public boolean isAllowParallel() {
		return false;
	}

}
